﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Project.Data;
using Project.Data.Repository;
using Project.Model.EF6.Entity;
using Project.Model.EF6.ViewModel;
using Project.Services.Tools;
using System.IO;

namespace Project.Services
{
    public static class ExecuteService
    {
        public static int GetData(DateTime date)
        {
            int count = 0;
            try
            {
                bool state = true;
                var yesterday = date; //  DateTime.Parse("2017-11-15");
                if (date != DateTime.Today.AddDays(-1))
                {
                    state = ClearData.Delete(date);
                }
                if (state)
                {
                    var begin = DateTime.Now;
                    var rep = new DbContextRepository<EfContext>();
                    string newPath = AppDomain.CurrentDomain.BaseDirectory + "\\";
                    if (!Directory.Exists(newPath))
                    {
                        Directory.CreateDirectory(newPath);
                    }
                    string path = newPath + "datetime.txt";
                    if (!File.Exists(path))
                    {
                        File.Create(path).Close();
                    }
                    var txt = File.ReadAllText(path);
                    if (!txt.Contains(yesterday.ToString("yyyy-MM-dd")))//|| date != DateTime.Today.AddDays(-1)
                    {
                        if (date == DateTime.Today.AddDays(-1))
                        {
                            (yesterday.ToString("yyyy-MM-dd") + "未执行过").WriteLog();
                            File.WriteAllText(path, yesterday.ToString("yyyy-MM-dd"), Encoding.UTF8);
                        }
                        var distributor = rep.FindAll<Distributor>(); //经销商
                        var distriView =
                            distributor.Select(m => new { m.DISCODE, m.DISNAME, m.POVINCIAL, m.CITY }).ToList();
                        var product = rep.FindAll<Product>();//所有产品数据
                        string[] productType = { "01", "04", "03", "1" };//产品分类
                        var productList = product.Select(m => new { m.PRODUCT_ID, m.PRODUCT_NAME, m.PRODUC_SPE_ID });
                        var provinceList = distriView.Select(m => m.POVINCIAL).Distinct().ToList();
                        var saleIdList = distriView.Select(m => m.DISCODE).Distinct().ToList();
                        var dayOfWeek = (int)yesterday.DayOfWeek;
                        //获取今天是今年第几周
                        GregorianCalendar gc = new GregorianCalendar();
                        int weekOfYear = gc.GetWeekOfYear(yesterday, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
                        bool isFirstDay = dayOfWeek == 1;

                        var firstDayOfMonth = DateTime.Today.AddDays(1 - DateTime.Today.Day);
                        bool isFirstDayOfMonth = firstDayOfMonth == yesterday;
                        var currMonth = yesterday.Month;

                        #region 48个半点统计

                        var dayCountSemih = new List<RedSemih>(); //每天48个半点红包发放次数
                        var dayAmountSemih = new List<AmountSemih>(); //每天48个半点红包发放金额
                        var dayPeopleSemih = new List<PeopleSemih>(); //每天48个半点红包参与人数
                        var dayPointSemih = new List<IntegralSemih>(); //每天48个半点积分参与人数
                        var daySalerSemih = new List<DistributorSemih>(); //每天48个半点经销商红包发放次数
                        var dayProductSemih = new List<ProductSemih>();//每天48个半点产品次数
                        var dayProductTypeSemih = new List<ProductSemih>(); //每天48个半点产品分类次数
                        var dayOpenid = new List<OpenIdViewModel>();
                        for (int i = 0; i < 48; i++)
                        {

                            var start = yesterday.AddMinutes(i * 30);
                            var end = yesterday.AddMinutes((i + 1) * 30);
                           (start+"~"+ end + "开始").WriteLog();

                            var list =
                        rep.QueryList<SalesViewModel>(
                            "Select ID,SALEID,REDPKT_AMOUNT,OPENID,POINT,PRODUC_SPE_ID,PRODUCT_ID, PRODUCT_ACTIVETIME,PRODUCT_NAME,SALENAME,SCAN_TIME From T_SGM_XLW_SALES_RECORD Where SCAN_TIME > TO_DATE('" +
                            start + "','yyyy-mm-dd hh24:mi:ss') And SCAN_TIME <=TO_DATE('" + end +
                            "','yyyy-mm-dd hh24:mi:ss')");
                            //("Select ID,SALEID,REDPKT_AMOUNT,OPENID,POINT,PRODUC_SPE_ID,PRODUCT_ID, PRODUCT_ACTIVETIME,PRODUCT_NAME,SALENAME,SCAN_TIME From T_SGM_XLW_SALES_RECORD Where SCAN_TIME > TO_DATE('" +
                            //start + "','yyyy-mm-dd hh24:mi:ss') And SCAN_TIME <=TO_DATE('" + end +
                            //"','yyyy-mm-dd hh24:mi:ss')").WriteLog();
                            var redSemih = new List<RedSemih>(); //半小时红包数量数据
                            var amountSemih = new List<AmountSemih>(); //半小时红包金额数据
                            var peopleSemih = new List<PeopleSemih>(); //半小时参与人数
                            var pointSemih = new List<IntegralSemih>(); //半小时积分数据
                            var distributorSemih = new List<DistributorSemih>(); //半小时经销商数据
                            var productSemih = new List<ProductSemih>(); //半小时产品数据
                            #region 省份统计
                            foreach (var n in provinceList)
                            {
                                var distribu = distriView.Where(m => m.POVINCIAL == n).ToList(); //当前省份的经销商
                                var discode = distribu.Select(m => m.DISCODE);
                                var sales = list.Where(m => discode.Contains(m.SALEID)).ToList();
                                dayOpenid.AddRange(sales.Select(r => new OpenIdViewModel
                                {
                                    Provice = n,
                                    OpenId = r.OPENID
                                }));
                                var red = new RedSemih
                                {
                                    PROVINCE = n,
                                    TYPE = i + 1,
                                    COUNT = sales.Count(),
                                    ROW_UPDATE_DATE = yesterday
                                };
                                redSemih.Add(red);
                                var money = new AmountSemih
                                {
                                    PROVINCE = n,
                                    TYPE = i + 1,
                                    AMOUNT = sales.Count == 0 ? 0 : (decimal)sales.Select(m => m.REDPKT_AMOUNT).Sum(),
                                    ROW_UPDATE_DATE = yesterday
                                };
                                amountSemih.Add(money);
                                var people = new PeopleSemih
                                {
                                    PROVINCE = n,
                                    TYPE = i + 1,
                                    COUNT = sales.Select(m => m.OPENID).Distinct().Count(),
                                    ROW_UPDATE_DATE = yesterday
                                };
                                peopleSemih.Add(people);

                                var point = new IntegralSemih
                                {
                                    PROVINCE = n,
                                    TYPE = i + 1,
                                    COUNT = sales.Count == 0 ? 0 : sales.Select(m => m.POINT).Sum(),
                                    ROW_UPDATE_DATE = yesterday
                                };
                                pointSemih.Add(point);

                                foreach (var r in distribu)
                                {
                                    var selerSales = sales.Where(m => m.SALEID == r.DISCODE);
                                    var saler = new DistributorSemih
                                    {
                                        PROVINCE =
                                            r.POVINCIAL + "-" + (string.IsNullOrEmpty(r.CITY) ? "" : r.CITY) + "-" +
                                            r.DISNAME + "-" + r.DISCODE,
                                        COUNT = selerSales.Count(),
                                        TYPE = i + 1,
                                        ROW_UPDATE_DATE = yesterday
                                    };
                                    distributorSemih.Add(saler);
                                }
                            }
                            #endregion

                            var proList = new List<ProductSemih>();
                            foreach (var n in productList)
                            {

                                var sales = list.Where(m => m.PRODUCT_ID == n.PRODUCT_ID).ToList();
                              

                                var semih = new ProductSemih
                                {
                                    PRODUCT_NAME = n.PRODUCT_NAME,
                                    TYPE = i + 1,
                                    COUNT = sales.Count,
                                    ROW_UPDATE_DATE = yesterday,
                                    PRODUC_SPE_ID = n.PRODUC_SPE_ID
                                };
                                productSemih.Add(semih);
                                proList.Add(semih);


                            }
                            dayProductSemih.AddRange(productSemih);
                            //统计每个产品分类半点扫码量
                                foreach (var n in productType)
                            {
                                List<ProductSemih> typeSales;
                                if (n == "1")
                                {
                                    typeSales = proList.Where(m => m.PRODUC_SPE_ID == "02" || m.PRODUC_SPE_ID == "06").ToList();
                                    var semih = new ProductSemih
                                    {
                                        PRODUCT_NAME = "九总",
                                        TYPE = i + 1,
                                        COUNT = typeSales.Select(m => m.COUNT).Sum(),
                                        ROW_UPDATE_DATE = yesterday
                                    };
                                    productSemih.Add(semih);
                                    dayProductTypeSemih.Add(semih);
                                }
                                else
                                {
                                    typeSales = proList.Where(m => m.PRODUC_SPE_ID == n).ToList();
                                    var semih = new ProductSemih
                                    {
                                        PRODUCT_NAME = "九总珍品",
                                        TYPE = i + 1,
                                        COUNT = typeSales.Select(m => m.COUNT).Sum(),
                                        ROW_UPDATE_DATE = yesterday
                                    };
                                    if (n == "01")
                                    {
                                        semih.PRODUCT_NAME = "一口怪兽";
                                    }
                                    else if (n == "03")
                                    {
                                        semih.PRODUCT_NAME = "九总珍品";
                                    }
                                    else if (n == "04")
                                    {
                                        semih.PRODUCT_NAME = "福和祥";
                                    }
                                    productSemih.Add(semih);
                                    dayProductTypeSemih.Add(semih);
                                }
                            }

                            rep.Add<RedSemih>(redSemih, false);
                            rep.Add<AmountSemih>(amountSemih, false);
                            rep.Add<PeopleSemih>(peopleSemih, false);
                            rep.Add<IntegralSemih>(pointSemih, false);
                            rep.Add<DistributorSemih>(distributorSemih, false);
                            rep.Add<ProductSemih>(productSemih, false);

                            var notIn = list.Where(m => !saleIdList.Contains(m.SALEID)).ToList(); //获取没有经销商的中奖纪录
                            //半点统计加入其他数据
                            var otherRedSemih = new RedSemih
                            {
                                PROVINCE = "其他",
                                TYPE = i + 1,
                                COUNT = notIn.Count,
                                ROW_UPDATE_DATE = yesterday
                            };
                            rep.Add<RedSemih>(otherRedSemih, false);
                            var otherDistributorSemih = new DistributorSemih //其他经销商
                            {
                                PROVINCE = "其他",
                                TYPE = i + 1,
                                COUNT = notIn.Count,
                                ROW_UPDATE_DATE = yesterday
                            };
                            rep.Add<DistributorSemih>(otherDistributorSemih, false);

                            var otherAmountSemih = new AmountSemih
                            {
                                PROVINCE = "其他",
                                TYPE = i + 1,
                                AMOUNT = notIn.Select(m => m.REDPKT_AMOUNT).Sum(),
                                ROW_UPDATE_DATE = yesterday
                            };
                            rep.Add<AmountSemih>(otherAmountSemih, false);
                            var otherPeopleSemih = new PeopleSemih
                            {
                                PROVINCE = "其他",
                                TYPE = i + 1,
                                COUNT = notIn.Select(m => m.OPENID).Distinct().Count(),
                                ROW_UPDATE_DATE = yesterday
                            };
                            rep.Add<PeopleSemih>(otherPeopleSemih, false);
                            dayOpenid.AddRange(notIn.Select(r => new OpenIdViewModel
                            {
                                Provice = "其他",
                                OpenId = r.OPENID
                            }));
                            var otherPointSemih = new IntegralSemih
                            {
                                PROVINCE = "其他",
                                TYPE = i + 1,
                                COUNT = notIn.Select(m => m.POINT).Sum(),
                                ROW_UPDATE_DATE = yesterday
                            };
                            rep.Add<IntegralSemih>(otherPointSemih, false);

                            //半点统计加入全国数据
                            var totalRedSemih = new RedSemih
                            {
                                PROVINCE = "全国",
                                TYPE = i + 1,
                                COUNT = redSemih.Select(m => m.COUNT).Sum() + notIn.Count,
                                ROW_UPDATE_DATE = yesterday
                            };
                            rep.Add<RedSemih>(totalRedSemih, false);

                            var totalAmountSemih = new AmountSemih
                            {
                                PROVINCE = "全国",
                                TYPE = i + 1,
                                AMOUNT =
                                    amountSemih.Select(m => m.AMOUNT).Sum() + notIn.Select(m => m.REDPKT_AMOUNT).Sum(),
                                ROW_UPDATE_DATE = yesterday
                            };
                            rep.Add<AmountSemih>(totalAmountSemih, false);

                            var totalPeopleSemih = new PeopleSemih
                            {
                                PROVINCE = "全国",
                                TYPE = i + 1,
                                //COUNT = peopleSemih.Select(m => m.COUNT).Sum() + otherPeopleSemih.COUNT,
                                COUNT = list.Select(m => m.OPENID).Distinct().Count(),
                                ROW_UPDATE_DATE = yesterday
                            };
                            rep.Add<PeopleSemih>(totalPeopleSemih, false);

                            var totalPointSemih = new IntegralSemih
                            {
                                PROVINCE = "全国",
                                TYPE = i + 1,
                                COUNT = pointSemih.Select(m => m.COUNT).Sum() + notIn.Select(m => m.POINT).Sum(),
                                ROW_UPDATE_DATE = yesterday
                            };
                            rep.Add<IntegralSemih>(totalPointSemih);

                            dayCountSemih.AddRange(redSemih);
                            dayAmountSemih.AddRange(amountSemih);
                            dayCountSemih.Add(otherRedSemih);
                            dayAmountSemih.Add(otherAmountSemih);
                            dayPeopleSemih.AddRange(peopleSemih);
                            dayPeopleSemih.Add(otherPeopleSemih);
                            dayPointSemih.AddRange(pointSemih);
                            dayPointSemih.Add(otherPointSemih);
                            daySalerSemih.AddRange(distributorSemih);
                            daySalerSemih.Add(otherDistributorSemih);
                            (start + "~" + end + "结束").WriteLog();
                        }
                        #endregion



                        #region  红包方法次数、红包金额、参与人数、积分数, 天、周、月统计

                        var redDay = new List<RedDay>(); //红包发放次数天数表
                        var amountDay = new List<AmountDay>(); //红包金额发放天数表
                        var peopleDay = new List<PeopleDay>(); //参与人数天数表
                        var pointDay = new List<IntegralDay>(); //积分发放天数表
                        var productDay = new List<ProductDay>();//产品扫码天数表
                        var dayCount = dayCountSemih;
                        var dayMoney = dayAmountSemih;
                        var dayProduct = dayProductSemih;
                        var dayProductType = dayProductTypeSemih;
                        var totalProvince = new List<string> { "其他" };
                        totalProvince.AddRange(provinceList);
                        foreach (var n in totalProvince)
                        {
                            var provinceDayCount = dayCount.Where(m => m.PROVINCE == n).ToList();
                            var provinceDayMoney = dayMoney.Where(m => m.PROVINCE == n).ToList();
                            //var provinceDayPeople = dayPeopleSemih.Where(m => m.PROVINCE == n).ToList();
                            var provinceDayPeople = dayOpenid.Where(m => m.Provice == n).ToList();
                            var provinceDayPoint = dayPointSemih.Where(m => m.PROVINCE == n).ToList();
                            redDay.Add(new RedDay
                            {
                                PROVINCE = n,
                                COUNT = provinceDayCount.Select(m => m.COUNT).Sum(),
                                ROW_UPDATE_DATE = yesterday
                            });
                            amountDay.Add(new AmountDay
                            {
                                PROVINCE = n,
                                AMOUNT = provinceDayMoney.Select(m => m.AMOUNT).Sum(),
                                ROW_UPDATE_DATE = yesterday
                            });
                            peopleDay.Add(new PeopleDay
                            {
                                PROVINCE = n,
                                COUNT = provinceDayPeople.Select(m => m.OpenId).Distinct().Count(),
                                ROW_UPDATE_DATE = yesterday
                            });
                            pointDay.Add(new IntegralDay
                            {
                                PROVINCE = n,
                                COUNT = provinceDayPoint.Select(m => m.COUNT).Sum(),
                                ROW_UPDATE_DATE = yesterday
                            });
                            var redWeek =
                                rep.Find<RedWeek>(
                                    m =>
                                        m.CURRENT_WEEK == weekOfYear && m.PROVINCE == n && yesterday >= m.WEEK_BEGIN &&
                                        yesterday < m.WEEK_END);
                            var amountWeek =
                                rep.Find<AmountWeek>(
                                    m =>
                                        m.CURRENT_WEEK == weekOfYear && m.PROVINCE == n && yesterday >= m.WEEK_BEGIN &&
                                        yesterday < m.WEEK_END);
                            var peopleWeek =
                                rep.Find<PeopleWeek>(
                                    m =>
                                        m.CURRENT_WEEK == weekOfYear && m.PROVINCE == n && yesterday >= m.WEEK_BEGIN &&
                                        yesterday < m.WEEK_END);
                            var pointWeek =
                                rep.Find<IntegralWeek>(
                                    m =>
                                        m.CURRENT_WEEK == weekOfYear && m.PROVINCE == n && yesterday >= m.WEEK_BEGIN &&
                                        yesterday < m.WEEK_END);
                            if (isFirstDay) //判断是否是每周第一天, 当前省份周数据统计
                            {
                                redWeek = new RedWeek
                                {
                                    COUNT = provinceDayCount.Select(m => m.COUNT).Sum(),
                                    WEEK_BEGIN = yesterday,
                                    CURRENT_WEEK = weekOfYear,
                                    ROW_UPDATE_DATE = yesterday,
                                    PROVINCE = n
                                };
                                redWeek.WEEK_END = redWeek.WEEK_BEGIN.AddDays(6);
                                rep.Add<RedWeek>(redWeek, false);

                                amountWeek = new AmountWeek
                                {
                                    PROVINCE = n,
                                    AMOUNT = provinceDayMoney.Select(m => m.AMOUNT).Sum(),
                                    WEEK_BEGIN = yesterday,
                                    WEEK_END = yesterday.AddDays(6),
                                    CURRENT_WEEK = weekOfYear,
                                    ROW_UPDATE_DATE = yesterday
                                };
                                amountWeek.WEEK_END = amountWeek.WEEK_BEGIN.AddDays(6);
                                rep.Add<AmountWeek>(amountWeek, false);

                                peopleWeek = new PeopleWeek
                                {
                                    COUNT = provinceDayPeople.Select(m => m.OpenId).Distinct().Count(),
                                    WEEK_BEGIN = yesterday,
                                    CURRENT_WEEK = weekOfYear,
                                    ROW_UPDATE_DATE = yesterday,
                                    PROVINCE = n
                                };
                                peopleWeek.WEEK_END = peopleWeek.WEEK_BEGIN.AddDays(6);
                                rep.Add<PeopleWeek>(peopleWeek, false);

                                pointWeek = new IntegralWeek
                                {
                                    PROVINCE = n,
                                    COUNT = provinceDayPoint.Select(m => m.COUNT).Sum(),
                                    WEEK_BEGIN = yesterday,
                                    WEEK_END = yesterday.AddDays(6),
                                    CURRENT_WEEK = weekOfYear,
                                    ROW_UPDATE_DATE = yesterday
                                };
                                pointWeek.WEEK_END = pointWeek.WEEK_BEGIN.AddDays(6);
                                rep.Add<IntegralWeek>(pointWeek, false);
                            }
                            else
                            {
                                if (redWeek == null)
                                {
                                    redWeek = new RedWeek
                                    {
                                        COUNT = provinceDayCount.Select(m => m.COUNT).Sum(),
                                        WEEK_BEGIN = yesterday.AddDays(1 - dayOfWeek),
                                        CURRENT_WEEK = weekOfYear,
                                        ROW_UPDATE_DATE = yesterday,
                                        PROVINCE = n
                                    };
                                    redWeek.WEEK_END = redWeek.WEEK_BEGIN.AddDays(6);
                                    rep.Add<RedWeek>(redWeek, false);
                                }
                                else
                                {
                                    redWeek.COUNT += provinceDayCount.Select(m => m.COUNT).Sum();
                                    rep.Update<RedWeek>(redWeek, false);
                                }
                                if (amountWeek == null)
                                {
                                    amountWeek = new AmountWeek
                                    {
                                        PROVINCE = n,
                                        AMOUNT = provinceDayMoney.Select(m => m.AMOUNT).Sum(),
                                        WEEK_BEGIN = yesterday.AddDays(1 - dayOfWeek),
                                        WEEK_END = yesterday.AddDays(6),
                                        CURRENT_WEEK = weekOfYear,
                                        ROW_UPDATE_DATE = yesterday
                                    };
                                    amountWeek.WEEK_END = amountWeek.WEEK_BEGIN.AddDays(6);
                                    rep.Add<AmountWeek>(amountWeek, false);
                                }
                                else
                                {
                                    amountWeek.AMOUNT += provinceDayMoney.Select(m => m.AMOUNT).Sum();
                                    rep.Update<AmountWeek>(amountWeek, false);
                                }

                                if (peopleWeek == null)
                                {
                                    peopleWeek = new PeopleWeek
                                    {
                                        COUNT = provinceDayPeople.Select(m => m.OpenId).Distinct().Count(),
                                        WEEK_BEGIN = yesterday.AddDays(1 - dayOfWeek),
                                        CURRENT_WEEK = weekOfYear,
                                        ROW_UPDATE_DATE = yesterday,
                                        PROVINCE = n
                                    };
                                    peopleWeek.WEEK_END = peopleWeek.WEEK_BEGIN.AddDays(6);
                                    rep.Add<PeopleWeek>(peopleWeek, false);
                                }
                                else
                                {
                                    peopleWeek.COUNT += provinceDayPeople.Select(m => m.OpenId).Distinct().Count();
                                    rep.Update<PeopleWeek>(peopleWeek, false);
                                }

                                if (pointWeek == null)
                                {
                                    pointWeek = new IntegralWeek
                                    {
                                        PROVINCE = n,
                                        COUNT = provinceDayPoint.Select(m => m.COUNT).Sum(),
                                        WEEK_BEGIN = yesterday.AddDays(1 - dayOfWeek),
                                        WEEK_END = yesterday.AddDays(6),
                                        CURRENT_WEEK = weekOfYear,
                                        ROW_UPDATE_DATE = yesterday
                                    };
                                    pointWeek.WEEK_END = pointWeek.WEEK_BEGIN.AddDays(6);
                                    rep.Add<IntegralWeek>(pointWeek, false);
                                }
                                else
                                {
                                    pointWeek.COUNT += provinceDayPoint.Select(m => m.COUNT).Sum();
                                    rep.Update<IntegralWeek>(pointWeek, false);
                                }
                            }
                            var redMonth =
                                rep.Find<RedMonth>(
                                    m =>
                                        m.PROVINCE == n && m.CURRENT_MONTH == currMonth && yesterday >= m.MONTH_BEGIN &&
                                        yesterday < m.MONTH_END);
                            var amountMonth =
                                rep.Find<AmountMonth>(
                                    m =>
                                        m.PROVINCE == n && m.CURRENT_MONTH == currMonth && yesterday >= m.MONTH_BEGIN &&
                                        yesterday < m.MONTH_END);
                            var peopleMonth =
                                rep.Find<PeopleMonth>(
                                    m =>
                                        m.PROVINCE == n && m.CURRENT_MONTH == currMonth && yesterday >= m.MONTH_BEGIN &&
                                        yesterday < m.MONTH_END);
                            var pointMonth =
                                rep.Find<IntegralMonth>(
                                    m =>
                                        m.PROVINCE == n && m.CURRENT_MONTH == currMonth && yesterday >= m.MONTH_BEGIN &&
                                        yesterday < m.MONTH_END);
                            if (isFirstDayOfMonth) //判断昨天是否是当前省当月第一天,当前月数据统计
                            {
                                redMonth = new RedMonth
                                {
                                    PROVINCE = n,
                                    COUNT = provinceDayCount.Select(m => m.COUNT).Sum(),
                                    MONTH_BEGIN = yesterday,
                                    MONTH_END = yesterday.AddMonths(1).AddDays(-1),
                                    ROW_UPDATE_DATE = yesterday,
                                    CURRENT_MONTH = yesterday.Month
                                };
                                rep.Add<RedMonth>(redMonth, false);

                                amountMonth = new AmountMonth
                                {
                                    PROVINCE = n,
                                    AMOUNT = provinceDayMoney.Select(m => m.AMOUNT).Sum(),
                                    MONTH_BEGIN = yesterday,
                                    MONTH_END = yesterday.AddMonths(1).AddDays(-1),
                                    ROW_UPDATE_DATE = yesterday,
                                    CURRENT_MONTH = yesterday.Month
                                };
                                rep.Add<AmountMonth>(amountMonth, false);

                                peopleMonth = new PeopleMonth
                                {
                                    PROVINCE = n,
                                    COUNT = provinceDayPeople.Select(m => m.OpenId).Distinct().Count(),
                                    MONTH_BEGIN = yesterday,
                                    MONTH_END = yesterday.AddMonths(1).AddDays(-1),
                                    ROW_UPDATE_DATE = yesterday,
                                    CURRENT_MONTH = yesterday.Month
                                };
                                rep.Add<PeopleMonth>(peopleMonth, false);

                                pointMonth = new IntegralMonth
                                {
                                    PROVINCE = n,
                                    COUNT = provinceDayPoint.Select(m => m.COUNT).Sum(),
                                    MONTH_BEGIN = yesterday,
                                    MONTH_END = yesterday.AddMonths(1).AddDays(-1),
                                    ROW_UPDATE_DATE = yesterday,
                                    CURRENT_MONTH = yesterday.Month
                                };
                                rep.Add<IntegralMonth>(pointMonth, false);
                            }
                            else
                            {
                                if (redMonth == null)
                                {
                                    redMonth = new RedMonth
                                    {
                                        PROVINCE = n,
                                        COUNT = provinceDayCount.Select(m => m.COUNT).Sum(),
                                        MONTH_BEGIN = yesterday.AddDays(1 - yesterday.Day),
                                        ROW_UPDATE_DATE = yesterday,
                                        CURRENT_MONTH = yesterday.Month
                                    };
                                    redMonth.MONTH_END = redMonth.MONTH_BEGIN.AddMonths(1).AddDays(-1);
                                    rep.Add<RedMonth>(redMonth, false);
                                }
                                else
                                {
                                    redMonth.COUNT += provinceDayCount.Select(m => m.COUNT).Sum();
                                    rep.Update<RedMonth>(redMonth, false);
                                }
                                if (amountMonth == null)
                                {
                                    amountMonth = new AmountMonth
                                    {
                                        PROVINCE = n,
                                        AMOUNT = provinceDayMoney.Select(m => m.AMOUNT).Sum(),
                                        MONTH_BEGIN = yesterday.AddDays(1 - yesterday.Day),
                                        ROW_UPDATE_DATE = yesterday,
                                        CURRENT_MONTH = yesterday.Month
                                    };
                                    amountMonth.MONTH_END = amountMonth.MONTH_BEGIN.AddMonths(1).AddDays(-1);
                                    rep.Add<AmountMonth>(amountMonth, false);
                                }
                                else
                                {
                                    amountMonth.AMOUNT += provinceDayMoney.Select(m => m.AMOUNT).Sum();
                                    rep.Update<AmountMonth>(amountMonth, false);
                                }

                                if (peopleMonth == null)
                                {
                                    peopleMonth = new PeopleMonth
                                    {
                                        PROVINCE = n,
                                        COUNT = provinceDayPeople.Select(m => m.OpenId).Distinct().Count(),
                                        MONTH_BEGIN = yesterday.AddDays(1 - yesterday.Day),
                                        ROW_UPDATE_DATE = yesterday,
                                        CURRENT_MONTH = yesterday.Month
                                    };
                                    peopleMonth.MONTH_END = peopleMonth.MONTH_BEGIN.AddMonths(1).AddDays(-1);
                                    rep.Add<PeopleMonth>(peopleMonth, false);
                                }
                                else
                                {
                                    peopleMonth.COUNT += provinceDayPeople.Select(m => m.OpenId).Distinct().Count();
                                    rep.Update<PeopleMonth>(peopleMonth, false);
                                }

                                if (pointMonth == null)
                                {
                                    pointMonth = new IntegralMonth
                                    {
                                        PROVINCE = n,
                                        COUNT = provinceDayPoint.Select(m => m.COUNT).Sum(),
                                        MONTH_BEGIN = yesterday.AddDays(1 - yesterday.Day),
                                        ROW_UPDATE_DATE = yesterday,
                                        CURRENT_MONTH = yesterday.Month
                                    };
                                    pointMonth.MONTH_END = pointMonth.MONTH_BEGIN.AddMonths(1).AddDays(-1);
                                    rep.Add<IntegralMonth>(pointMonth, false);
                                }
                                else
                                {
                                    pointMonth.COUNT += provinceDayPoint.Select(m => m.COUNT).Sum();
                                    rep.Update<IntegralMonth>(pointMonth, false);
                                }
                            }
                        }
                        foreach (var n in productList)
                        {
                            var productDayCount = dayProduct.Where(m => m.PRODUCT_NAME == n.PRODUCT_NAME).ToList();
                            productDay.Add(new ProductDay
                            {
                                PRODUCT_NAME = n.PRODUCT_NAME,
                                COUNT = productDayCount.Select(m => m.COUNT).Sum(),
                                ROW_UPDATE_DATE = yesterday
                            });
                            var productWeek =
                               rep.Find<ProductWeek>(
                                   m =>
                                       m.CURRENT_WEEK == weekOfYear && m.PRODUCT_NAME == n.PRODUCT_NAME && yesterday >= m.WEEK_BEGIN &&
                                       yesterday < m.WEEK_END);
                            if (isFirstDay) //判断是否是每周第一天, 当前省份周数据统计
                            {
                                productWeek = new ProductWeek
                                {
                                    COUNT = productDayCount.Select(m => m.COUNT).Sum(),
                                    WEEK_BEGIN = yesterday,
                                    CURRENT_WEEK = weekOfYear,
                                    ROW_UPDATE_DATE = yesterday,
                                    PRODUCT_NAME = n.PRODUCT_NAME
                                };
                                productWeek.WEEK_END = productWeek.WEEK_BEGIN.AddDays(6);
                                rep.Add<ProductWeek>(productWeek, false);
                            }
                            else
                            {
                                if (productWeek == null)
                                {
                                    productWeek = new ProductWeek
                                    {
                                        COUNT = productDayCount.Select(m => m.COUNT).Sum(),
                                        WEEK_BEGIN = yesterday.AddDays(1 - dayOfWeek),
                                        CURRENT_WEEK = weekOfYear,
                                        ROW_UPDATE_DATE = yesterday,
                                        PRODUCT_NAME = n.PRODUCT_NAME
                                    };
                                    productWeek.WEEK_END = productWeek.WEEK_BEGIN.AddDays(6);
                                    rep.Add<ProductWeek>(productWeek, false);
                                }
                                else
                                {
                                    productWeek.COUNT += productDayCount.Select(m => m.COUNT).Sum();
                                    rep.Update<ProductWeek>(productWeek, false);
                                }
                            }

                            var productMonth =
                                rep.Find<ProductMonth>(
                                    m =>
                                        m.PRODUCT_NAME == n.PRODUCT_NAME && m.CURRENT_MONTH == currMonth && yesterday >= m.MONTH_BEGIN &&
                                        yesterday < m.MONTH_END);
                            if (isFirstDayOfMonth) //判断昨天是否是当前省当月第一天,当前月数据统计
                            {
                                productMonth = new ProductMonth
                                {
                                    PRODUCT_NAME = n.PRODUCT_NAME,
                                    COUNT = productDayCount.Select(m => m.COUNT).Sum(),
                                    MONTH_BEGIN = yesterday,
                                    MONTH_END = yesterday.AddMonths(1).AddDays(-1),
                                    ROW_UPDATE_DATE = yesterday,
                                    CURRENT_MONTH = yesterday.Month
                                };
                                rep.Add<ProductMonth>(productMonth, false);
                            }
                            else
                            {
                                if (productMonth == null)
                                {
                                    productMonth = new ProductMonth
                                    {
                                        PRODUCT_NAME = n.PRODUCT_NAME,
                                        COUNT = productDayCount.Select(m => m.COUNT).Sum(),
                                        MONTH_BEGIN = yesterday.AddDays(1 - yesterday.Day),
                                        ROW_UPDATE_DATE = yesterday,
                                        CURRENT_MONTH = yesterday.Month
                                    };
                                    productMonth.MONTH_END = productMonth.MONTH_BEGIN.AddMonths(1).AddDays(-1);
                                    rep.Add<ProductMonth>(productMonth, false);
                                }
                                else
                                {
                                    productMonth.COUNT += productDayCount.Select(m => m.COUNT).Sum();
                                    rep.Update<ProductMonth>(productMonth, false);
                                }
                            }
                        }
                        //产品分类统计
                        string[] type = { "九总", "九总珍品", "一口怪兽", "福和祥" };
                        foreach (var n in type)
                        {
                            var productTypeDayCount = dayProductType.Where(m => m.PRODUCT_NAME == n).ToList();
                            productDay.Add(new ProductDay
                            {
                                PRODUCT_NAME = n,
                                COUNT = productTypeDayCount.Select(m => m.COUNT).Sum(),
                                ROW_UPDATE_DATE = yesterday
                            });
                            var productWeek =
                               rep.Find<ProductWeek>(
                                   m =>
                                       m.CURRENT_WEEK == weekOfYear && m.PRODUCT_NAME == n && yesterday >= m.WEEK_BEGIN &&
                                       yesterday < m.WEEK_END);
                            if (isFirstDay) //判断是否是每周第一天, 当前省份周数据统计
                            {
                                productWeek = new ProductWeek
                                {
                                    COUNT = productTypeDayCount.Select(m => m.COUNT).Sum(),
                                    WEEK_BEGIN = yesterday,
                                    CURRENT_WEEK = weekOfYear,
                                    ROW_UPDATE_DATE = yesterday,
                                    PRODUCT_NAME = n
                                };
                                productWeek.WEEK_END = productWeek.WEEK_BEGIN.AddDays(6);
                                rep.Add<ProductWeek>(productWeek, false);
                            }
                            else
                            {
                                if (productWeek == null)
                                {
                                    productWeek = new ProductWeek
                                    {
                                        COUNT = productTypeDayCount.Select(m => m.COUNT).Sum(),
                                        WEEK_BEGIN = yesterday.AddDays(1 - dayOfWeek),
                                        CURRENT_WEEK = weekOfYear,
                                        ROW_UPDATE_DATE = yesterday,
                                        PRODUCT_NAME = n
                                    };
                                    productWeek.WEEK_END = productWeek.WEEK_BEGIN.AddDays(6);
                                    rep.Add<ProductWeek>(productWeek, false);
                                }
                                else
                                {
                                    productWeek.COUNT += productTypeDayCount.Select(m => m.COUNT).Sum();
                                    rep.Update<ProductWeek>(productWeek, false);
                                }
                            }

                            var productMonth =
                                rep.Find<ProductMonth>(
                                    m =>
                                        m.PRODUCT_NAME == n && m.CURRENT_MONTH == currMonth && yesterday >= m.MONTH_BEGIN &&
                                        yesterday < m.MONTH_END);
                            if (isFirstDayOfMonth) //判断昨天是否是当前省当月第一天,当前月数据统计
                            {
                                productMonth = new ProductMonth
                                {
                                    PRODUCT_NAME = n,
                                    COUNT = productTypeDayCount.Select(m => m.COUNT).Sum(),
                                    MONTH_BEGIN = yesterday,
                                    MONTH_END = yesterday.AddMonths(1).AddDays(-1),
                                    ROW_UPDATE_DATE = yesterday,
                                    CURRENT_MONTH = yesterday.Month
                                };
                                rep.Add<ProductMonth>(productMonth, false);
                            }
                            else
                            {
                                if (productMonth == null)
                                {
                                    productMonth = new ProductMonth
                                    {
                                        PRODUCT_NAME = n,
                                        COUNT = productTypeDayCount.Select(m => m.COUNT).Sum(),
                                        MONTH_BEGIN = yesterday.AddDays(1 - yesterday.Day),
                                        ROW_UPDATE_DATE = yesterday,
                                        CURRENT_MONTH = yesterday.Month
                                    };
                                    productMonth.MONTH_END = productMonth.MONTH_BEGIN.AddMonths(1).AddDays(-1);
                                    rep.Add<ProductMonth>(productMonth, false);
                                }
                                else
                                {
                                    productMonth.COUNT += productTypeDayCount.Select(m => m.COUNT).Sum();
                                    rep.Update<ProductMonth>(productMonth, false);
                                }
                            }
                        }

                        #endregion

                        #region  经销商天、周、月统计

                        //经销商天、周、月统计
                        var salerList = daySalerSemih.Select(m => m.PROVINCE).Distinct();
                        var distributorDay = new List<DistributorDay>(); //经销商天数表
                        foreach (var n in salerList)
                        {
                            var salerModel = daySalerSemih.Where(m => m.PROVINCE == n).ToList();
                            distributorDay.Add(new DistributorDay
                            {
                                PROVINCE = n,
                                COUNT = salerModel.Select(m => m.COUNT).Sum(),
                                ROW_UPDATE_DATE = yesterday
                            });
                            //经销商周表
                            var distributorWeek =
                                rep.Find<DistributorWeek>(
                                    m =>
                                        m.CURRENT_WEEK == weekOfYear && m.PROVINCE == n && yesterday >= m.WEEK_BEGIN &&
                                        yesterday < m.WEEK_END);
                            if (isFirstDay) //判断是否是每周第一天, 当前省份周数据统计
                            {
                                distributorWeek = new DistributorWeek
                                {
                                    PROVINCE = n,
                                    COUNT = salerModel.Select(m => m.COUNT).Sum(),
                                    WEEK_BEGIN = yesterday,
                                    WEEK_END = yesterday.AddDays(6),
                                    CURRENT_WEEK = weekOfYear,
                                    ROW_UPDATE_DATE = yesterday
                                };
                                distributorWeek.WEEK_END = distributorWeek.WEEK_BEGIN.AddDays(6);
                                rep.Add<DistributorWeek>(distributorWeek, false);
                            }
                            else
                            {
                                if (distributorWeek == null)
                                {
                                    distributorWeek = new DistributorWeek
                                    {
                                        COUNT = salerModel.Select(m => m.COUNT).Sum(),
                                        WEEK_BEGIN = yesterday.AddDays(1 - dayOfWeek),
                                        CURRENT_WEEK = weekOfYear,
                                        ROW_UPDATE_DATE = yesterday,
                                        PROVINCE = n
                                    };
                                    distributorWeek.WEEK_END = distributorWeek.WEEK_BEGIN.AddDays(6);
                                    rep.Add<DistributorWeek>(distributorWeek, false);
                                }
                                else
                                {
                                    distributorWeek.COUNT += salerModel.Select(m => m.COUNT).Sum();
                                    rep.Update<DistributorWeek>(distributorWeek, false);
                                }
                            }
                            //经销商月表
                            var distributorMonth =
                                rep.Find<DistributorMonth>(
                                    m =>
                                        m.PROVINCE == n && m.CURRENT_MONTH == currMonth && yesterday >= m.MONTH_BEGIN &&
                                        yesterday < m.MONTH_END);
                            if (isFirstDayOfMonth) //判断昨天是否是当前省当月第一天,当前月数据统计
                            {
                                distributorMonth = new DistributorMonth
                                {
                                    PROVINCE = n,
                                    COUNT = salerModel.Select(m => m.COUNT).Sum(),
                                    MONTH_BEGIN = yesterday,
                                    MONTH_END = yesterday.AddMonths(1).AddDays(-1),
                                    ROW_UPDATE_DATE = yesterday,
                                    CURRENT_MONTH = yesterday.Month
                                };
                                rep.Add<DistributorMonth>(distributorMonth, false);
                            }
                            else
                            {
                                if (distributorMonth == null)
                                {
                                    distributorMonth = new DistributorMonth
                                    {
                                        PROVINCE = n,
                                        COUNT = salerModel.Select(m => m.COUNT).Sum(),
                                        MONTH_BEGIN = yesterday.AddDays(1 - yesterday.Day),
                                        ROW_UPDATE_DATE = yesterday,
                                        CURRENT_MONTH = yesterday.Month
                                    };
                                    distributorMonth.MONTH_END = distributorMonth.MONTH_BEGIN.AddMonths(1).AddDays(-1);
                                    rep.Add<DistributorMonth>(distributorMonth, false);
                                }
                                else
                                {
                                    distributorMonth.COUNT += salerModel.Select(m => m.COUNT).Sum();
                                    rep.Update<DistributorMonth>(distributorMonth, false);
                                }
                            }
                        }

                        #endregion

                        #region 全国数据统计

                        int weekProvinceRedCount = dayCount.Select(m => m.COUNT).Sum(); //当天所有扫码量
                        decimal weekProvinceRedAmount = dayMoney.Select(m => m.AMOUNT).Sum(); //当天所有红包金额
                        //int weekProvincePeople = dayPeopleSemih.Select(m => m.COUNT).Sum(); //当天用户数
                        int weekProvincePeople = dayOpenid.Select(m => m.OpenId).Distinct().Count(); //当天用户数
                        int weekProvincePoint = dayPointSemih.Select(m => m.COUNT).Sum(); //当天所有积分
                        //为全国统计每天红包数据
                        var totalRedDay = rep.Find<RedDay>(m => m.PROVINCE == "全国" && m.ROW_UPDATE_DATE == yesterday);
                        if (totalRedDay != null)
                        {
                            totalRedDay.COUNT += weekProvinceRedCount;
                            rep.Update(totalRedDay, false);
                        }
                        else
                        {
                            var newTotalRedDay = new RedDay
                            {
                                PROVINCE = "全国",
                                ROW_UPDATE_DATE = yesterday,
                                COUNT = weekProvinceRedCount
                            };
                            redDay.Add(newTotalRedDay);
                        }

                        var totalAmountDay =
                            rep.Find<AmountDay>(m => m.PROVINCE == "全国" && m.ROW_UPDATE_DATE == yesterday);
                        if (totalAmountDay != null)
                        {
                            totalAmountDay.AMOUNT += weekProvinceRedAmount;
                            rep.Update(totalAmountDay, false);
                        }
                        else
                        {
                            var newTotalRedDay = new AmountDay
                            {
                                PROVINCE = "全国",
                                ROW_UPDATE_DATE = yesterday,
                                AMOUNT = weekProvinceRedAmount
                            };
                            amountDay.Add(newTotalRedDay);
                        }

                        //为全国统计每天人数
                        var totalPeopleDay =
                            rep.Find<PeopleDay>(m => m.PROVINCE == "全国" && m.ROW_UPDATE_DATE == yesterday);
                        if (totalPeopleDay != null)
                        {
                            totalPeopleDay.COUNT += weekProvincePeople;
                            rep.Update(totalPeopleDay, false);
                        }
                        else
                        {
                            var newTotalPeopleDay = new PeopleDay
                            {
                                PROVINCE = "全国",
                                ROW_UPDATE_DATE = yesterday,
                                COUNT = weekProvincePeople
                            };
                            peopleDay.Add(newTotalPeopleDay);
                        }
                        //为全国统计每天积分数
                        var totalPointDay =
                            rep.Find<IntegralDay>(m => m.PROVINCE == "全国" && m.ROW_UPDATE_DATE == yesterday);
                        if (totalPointDay != null)
                        {
                            totalPointDay.COUNT += weekProvincePoint;
                            rep.Update(totalPointDay, false);
                        }
                        else
                        {
                            var newTotalPointDay = new IntegralDay
                            {
                                PROVINCE = "全国",
                                ROW_UPDATE_DATE = yesterday,
                                COUNT = weekProvincePoint
                            };
                            pointDay.Add(newTotalPointDay);
                        }

                        //统计全国当前周红包发放次数数据
                        var totalWeekCount = rep.Find<RedWeek>(m => m.PROVINCE == "全国" && m.CURRENT_WEEK == weekOfYear);
                        if (totalWeekCount == null)
                        {
                            var totalWeekRedCount = new RedWeek
                            {
                                PROVINCE = "全国",
                                COUNT = weekProvinceRedCount,
                                ROW_UPDATE_DATE = yesterday,
                                WEEK_BEGIN = yesterday.AddDays(1 - dayOfWeek),
                                WEEK_END = yesterday.AddDays(6),
                                CURRENT_WEEK = weekOfYear
                            };
                            totalWeekRedCount.WEEK_END = totalWeekRedCount.WEEK_BEGIN.AddDays(6);
                            rep.Add<RedWeek>(totalWeekRedCount, false);
                        }
                        else
                        {
                            totalWeekCount.COUNT += weekProvinceRedCount;
                            rep.Update<RedWeek>(totalWeekCount, false);
                        }
                        //统计全国当前周红包发放金额数据
                        var totalWeekAmount =
                            rep.Find<AmountWeek>(m => m.PROVINCE == "全国" && m.CURRENT_WEEK == weekOfYear);
                        if (totalWeekAmount == null)
                        {
                            var totalWeekRedAmount = new AmountWeek
                            {
                                PROVINCE = "全国",
                                AMOUNT = weekProvinceRedAmount,
                                ROW_UPDATE_DATE = yesterday,
                                WEEK_BEGIN = yesterday.AddDays(1 - dayOfWeek),
                                CURRENT_WEEK = weekOfYear
                            };
                            totalWeekRedAmount.WEEK_END = totalWeekRedAmount.WEEK_BEGIN.AddDays(6);
                            rep.Add<AmountWeek>(totalWeekRedAmount, false);
                        }
                        else
                        {
                            totalWeekAmount.AMOUNT += weekProvinceRedAmount;
                            rep.Update<AmountWeek>(totalWeekAmount, false);
                        }

                        //统计全国当前周人数数据
                        var totalWeekPeople =
                            rep.Find<PeopleWeek>(m => m.PROVINCE == "全国" && m.CURRENT_WEEK == weekOfYear);
                        if (totalWeekPeople == null)
                        {
                            var newTotalWeekPeople = new PeopleWeek
                            {
                                PROVINCE = "全国",
                                COUNT = weekProvincePeople,
                                ROW_UPDATE_DATE = yesterday,
                                WEEK_BEGIN = yesterday.AddDays(1 - dayOfWeek),
                                WEEK_END = yesterday.AddDays(6),
                                CURRENT_WEEK = weekOfYear
                            };
                            newTotalWeekPeople.WEEK_END = newTotalWeekPeople.WEEK_BEGIN.AddDays(6);
                            rep.Add<PeopleWeek>(newTotalWeekPeople, false);
                        }
                        else
                        {
                            totalWeekPeople.COUNT += weekProvincePeople;
                            rep.Update<PeopleWeek>(totalWeekPeople, false);
                        }
                        //统计全国当前周积分发放金额数据
                        var totalWeekPoint =
                            rep.Find<IntegralWeek>(m => m.PROVINCE == "全国" && m.CURRENT_WEEK == weekOfYear);
                        if (totalWeekPoint == null)
                        {
                            var newTotalWeekPoint = new IntegralWeek
                            {
                                PROVINCE = "全国",
                                COUNT = weekProvincePoint,
                                ROW_UPDATE_DATE = yesterday,
                                WEEK_BEGIN = yesterday.AddDays(1 - dayOfWeek),
                                CURRENT_WEEK = weekOfYear
                            };
                            newTotalWeekPoint.WEEK_END = newTotalWeekPoint.WEEK_BEGIN.AddDays(6);
                            rep.Add<IntegralWeek>(newTotalWeekPoint, false);
                        }
                        else
                        {
                            totalWeekPoint.COUNT += weekProvincePoint;
                            rep.Update<IntegralWeek>(totalWeekPoint, false);
                        }

                        //统计全国当前月红包发放次数数据
                        var totalMonthCount = rep.Find<RedMonth>(m => m.PROVINCE == "全国" && m.CURRENT_MONTH == currMonth);
                        if (totalMonthCount == null)
                        {
                            var totalMonthRedCount = new RedMonth
                            {
                                PROVINCE = "全国",
                                COUNT = weekProvinceRedCount,
                                ROW_UPDATE_DATE = yesterday,
                                MONTH_BEGIN = yesterday.AddDays(1 - yesterday.Day),
                                CURRENT_MONTH = currMonth
                            };
                            totalMonthRedCount.MONTH_END = totalMonthRedCount.MONTH_BEGIN.AddMonths(1).AddDays(-1);
                            rep.Add<RedMonth>(totalMonthRedCount, false);
                        }
                        else
                        {
                            totalMonthCount.COUNT += weekProvinceRedCount;
                            rep.Update<RedMonth>(totalMonthCount, false);
                        }

                        //统计全国当前月红包发放金额数据
                        var totalMonthAmount =
                            rep.Find<AmountMonth>(m => m.PROVINCE == "全国" && m.CURRENT_MONTH == currMonth);
                        if (totalMonthAmount == null)
                        {
                            var totalWeekRedAmount = new AmountMonth
                            {
                                PROVINCE = "全国",
                                AMOUNT = weekProvinceRedAmount,
                                ROW_UPDATE_DATE = yesterday,
                                MONTH_BEGIN = yesterday.AddDays(1 - yesterday.Day),
                                CURRENT_MONTH = currMonth
                            };
                            totalWeekRedAmount.MONTH_END = totalWeekRedAmount.MONTH_BEGIN.AddMonths(1).AddDays(-1);
                            rep.Add<AmountMonth>(totalWeekRedAmount, false);
                        }
                        else
                        {
                            totalMonthAmount.AMOUNT += weekProvinceRedAmount;
                            rep.Update<AmountMonth>(totalMonthAmount, false);
                        }

                        //统计全国当前月参与人次数据
                        var totalMonthPeople =
                            rep.Find<PeopleMonth>(m => m.PROVINCE == "全国" && m.CURRENT_MONTH == currMonth);
                        if (totalMonthPeople == null)
                        {
                            var newTotalMonthPeople = new PeopleMonth
                            {
                                PROVINCE = "全国",
                                COUNT = weekProvincePeople,
                                ROW_UPDATE_DATE = yesterday,
                                MONTH_BEGIN = yesterday.AddDays(1 - yesterday.Day),
                                CURRENT_MONTH = currMonth
                            };
                            newTotalMonthPeople.MONTH_END = newTotalMonthPeople.MONTH_BEGIN.AddMonths(1).AddDays(-1);
                            rep.Add<PeopleMonth>(newTotalMonthPeople, false);
                        }
                        else
                        {
                            totalMonthPeople.COUNT += weekProvincePeople;
                            rep.Update<PeopleMonth>(totalMonthPeople, false);
                        }

                        //统计全国当前月积分发放数据
                        var totalMonthPoint =
                            rep.Find<IntegralMonth>(m => m.PROVINCE == "全国" && m.CURRENT_MONTH == currMonth);
                        if (totalMonthPoint == null)
                        {
                            var newTotalMonthPoint = new IntegralMonth
                            {
                                PROVINCE = "全国",
                                COUNT = weekProvincePoint,
                                ROW_UPDATE_DATE = yesterday,
                                MONTH_BEGIN = yesterday.AddDays(1 - yesterday.Day),
                                CURRENT_MONTH = currMonth
                            };
                            newTotalMonthPoint.MONTH_END = newTotalMonthPoint.MONTH_BEGIN.AddMonths(1).AddDays(-1);
                            rep.Add<IntegralMonth>(newTotalMonthPoint, false);
                        }
                        else
                        {
                            totalMonthPoint.COUNT += weekProvincePoint;
                            rep.Update<IntegralMonth>(totalMonthPoint, false);
                        }

                        #endregion

                        var redDayFlag = rep.Add<RedDay>(redDay);
                        var amountDayFlag = rep.Add<AmountDay>(amountDay);
                        var peopleDayFlag = rep.Add<PeopleDay>(peopleDay);
                        var pointDayFlag = rep.Add<IntegralDay>(pointDay);
                        var distributorDayFlag = rep.Add<DistributorDay>(distributorDay);
                        var productDayFlag = rep.Add<ProductDay>(productDay);

                        //只更新昨天的用户粘性
                        if (date == DateTime.Today.AddDays(-1))
                        {
                            PeopleService.GetData(date);
                        }
                        ("redDayFlag:" + redDayFlag).WriteLog();
                        ("amountDayFlag:" + amountDayFlag).WriteLog();
                        count = redDay.Count;
                    }
                    else
                    {
                        (yesterday.ToString("yyyy-MM-dd") + "已执行过").WriteLog();
                    }
                    var endDate = DateTime.Now;
                    return count;
                }
                else
                {
                    ("数据清除未成功").WriteLog();
                    return 0;
                }

                //return (count+",begin:"+begin+",end:"+ endDate);
            }
            catch (Exception ex)
            {
                ex.ErrorHandle();
                return 0;
            }
        }
    }
}
